<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005-2009 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  Leo Feyer 2005-2009
 * @author     Leo Feyer <leo@typolight.org>
 * @package    Backend
 * @license    LGPL
 * @filesource
 */


/**
 * Class ModuleMaintenance
 *
 * Back end module "maintenance".
 * @copyright  Leo Feyer 2005-2009
 * @author     Leo Feyer <leo@typolight.org>
 * @package    Controller
 */
class ModuleInvoice extends BackendModule
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'be_memberlist';


	/**
	 * Generate module
	 */
	protected function compile()
	{
		if (!$this->Input->get('id_order'))
		{
			return;
		}
		
		$id_order = $this->Input->get('id_order');
		
		$this->Template = new BackendTemplate("mod_invoice");
		$this->obj_currency = new Currency();
		
		$objOders = $this->Database->prepare("SELECT IF(odr.dly_firstname<>'', odr.dly_firstname, addrbook.firstname) AS firstname, IF(odr.dly_lastname<>'', odr.dly_lastname, addrbook.lastname) AS lastname, IF(odr.dly_street<>'', odr.dly_street, addrbook.street) AS shippingAddr, IF(odr.dly_street_2<>'', odr.dly_street_2, addrbook.street_2) AS shippingAddr2, IF(odr.dly_city<>'', odr.dly_city, addrbook.city) AS city, IF(odr.dly_state<>'', odr.dly_state, addrbook.state) AS state, IF(odr.dly_country<>'', odr.dly_country, addrbook.country) AS country, IF(odr.dly_postcode<>'', odr.dly_postcode, addrbook.postcode) AS postcode, IF(odr.dly_telephone<>'', odr.dly_telephone, addrbook.telephone) AS telephone, IF(odr.dly_cellphone<>'', odr.dly_cellphone, addrbook.cellphone) AS cellphone, 
								odd.product_price AS odrPrice, odr.payment AS paymethod, odr.total_discounts, odr.orderno AS orderno, odr.total_wrapping, 
								odr.total_catolog, odr.total_paid_real, odr.total_paid, odr.date_add AS placed_time, odr.total_shipping AS shipping_fee, 
								odr.total_catolog AS product_fee, odr.`odr_status`, odd.product_attribute AS product_attr, IF(prod_cata.skuid, prod_cata.skuid, odd.product_skuid) AS skuid, odr.`odr_comments`AS comments, prod_cata.category AS category, prod_cata.id AS id_catalog, IF(prod_cata.name, prod_cata.name, odd.product_name) AS catalogName, 
								odd.product_quantity AS prod_quantity, odd.is_package AS is_wrapping, odd.gwPrice AS wrapping_price, me.`username`, me.`email`, odr.`id_member`,cur.iso_code 
								FROM tl_orders AS odr
								LEFT JOIN tl_order_detail AS odd ON odd.pid=odr.id
								LEFT JOIN tl_address_book AS addrbook ON addrbook.id=odr.id_address_delivery
								LEFT JOIN tl_member AS me ON me.id=odr.id_member
								LEFT JOIN product_catalog AS prod_cata ON prod_cata.id=odd.product_catalog_id
								LEFT JOIN tl_currency AS cur ON cur.id=odr.id_currency 
								WHERE odr.id=?")
						->execute($id_order);
		if ($objOders->numRows < 1)
			return;
		
		$arrOrderdetails = array();
		
		while ($objOders->next())
		{
			$shippingFree = $objOders->shipping_fee;
			$total_discounts = $objOders->total_discounts;
			
			$arrColor = explode("\n", $objOders->product_attr);
			$arrColor_bk = explode(":", $arrColor[0]);
			$strColor = $arrColor_bk[1];
			$arrSize_bk = explode(":", $arrColor[1]);
			$strSize = $arrSize_bk[1];
			
			$objCatalogSize = $this->Database->prepare("SELECT `id` FROM `catalog_size` WHERE `size`=?")
								->execute($strSize);
								
			$objCatalogColor = $this->Database->prepare("SELECT `id` FROM `catalog_colour` WHERE `title`=?")
								->execute($strColor);
			
			$imgPath = "daft_files/catalog/products/" . $objOders->skuid . "/$strColor/1.jpg";
			
			$arrOrderdetails[] = array(
				'firstname' => $objOders->firstname,
				'lastname' => $objOders->lastname,
				'shippingAddr' => $objOders->shippingAddr,
				'shippingAddr2' => $objOders->shippingAddr2,
				'city' => $objOders->city,
				'state' => $objOders->state,
				'country' => $objOders->country,
				'postcode' => $objOders->postcode,
				'odrPrice' => $objOders->odrPrice,
				'paymethod' => $objOders->paymethod,
				'total_discounts' => $objOders->total_discounts,
				'orderno' => $objOders->orderno,
				'total_wrapping' => $objOders->total_wrapping,
				'total_catolog' => $objOders->total_catolog,
				'total_paid_real' => $objOders->total_paid_real,
				'total_paid' => $objOders->total_paid,
				'placed_time' => $objOders->placed_time,
				'shipping_fee' => $objOders->shipping_fee,
				'product_fee' => $objOders->product_fee,
				'product_attr' => $objOders->product_attr,
				'skuid' => $objOders->skuid,
				//'stock' => $objOders->stock,
				'category' => $objOders->category,
				'id_catalog' => $objOders->id_catalog,
				'catalogName' => $objOders->catalogName,
				'prod_quantity' => $objOders->prod_quantity,
				'is_wrapping' => $objOders->is_wrapping,
				'wrapping_price' => $objOders->wrapping_price,
				'id_color' => $objCatalogColor->id,
				'colorName' => $strColor,
				'id_size' => $objCatalogSize->id,
				'sizeName' => $strSize,
				'imagePath' => $imgPath,
				'shippingFree' => $shippingFree,
				'total_discounts' => $total_discounts,
				'odr_status' => $objOders->odr_status,
				'id_order' => $id_order,
				'username' => $objOders->username,
				'email' => $objOders->email,
				'id_member' => $objOders->id_member,
				'comments' => $objOders->comments,
				'telephone' => $objOders->telephone,
				'cellphone' => $objOders->cellphone,
				'iso_code' => $objOders->iso_code,
			);
		}
		
		$this->loadLanguageFile('countries');
		include(TL_ROOT . '/system/config/countries.php');

		// Get the states.
		$arrStates = array();
		$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
									->execute(time());
		while ($objStates->next())
		{
			$arrStates[$objStates->id] = stripslashes($objStates->province_en);
		}
		
		// Get city.
		$arrCitys = array();			
		$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `city_en`")
									->execute(time());
									
		while ($objCitys->next())
		{
			$arrCitys[$objCitys->id] = stripslashes($objCitys->city_en);
		}
		
		$this->Template->arrStates = $arrStates;
		$this->Template->arrCitys = $arrCitys;
		$this->Template->arrCountries = $countries;
		$this->Template->basePath = $this->Environment->base;		
		$this->Template->orderDetails = $arrOrderdetails;
		$this->Template->obj_currency = $this->obj_currency;
	}
}

?>